package tp.pr5.instructions;


import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.ItemContainer;

/**
 * Its execution request the robot to finish the simulation This Instruction works if the user writes QUIT or SALIR
 *
 */
public class QuitInstruction implements Instruction
{

	private RobotEngine engine;

	/**
	 * Default constructor
	 */
	public QuitInstruction()
	{

	}

	/**
	 * Parses the String returning an instance of QuitInstruction or throwing a WrongInstructionFormatException()
	 * 
	 * @param cad string given
	 */
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException 
	{
		cad = cad.toUpperCase();
		if(cad.equals("QUIT") || cad.equals("SALIR"))
		{
			return new QuitInstruction();
		}
		throw new WrongInstructionFormatException();
	}

	
	/**
	 * Fixes the current context, i.e., the robot engine and the navigation module
	 * 
	 * @param engine RobotEngine
	 * @param navigation NavigationModule
	 * @param robotContainer ItemContainer
	 * 
	 */
	@Override
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer)
	{
		this.engine = engine;
	}

	/**
	 * Request the robot to stop the simulation
	 */
	@Override
	public void execute() throws InstructionExecutionException
	{
		try
		{
			this.engine.requestQuit();

		} catch(Exception e){
			throw new InstructionExecutionException();
		}
		
	}

	/**
	 * Returns a description of the Instruction syntax. The string does not end with the line separator. It is up to the caller adding it before printing.
	 */
	@Override
	public String getHelp() 
	{
		return "QUIT|SALIR";
	}

}
